package com.cyx.controller;

import com.cyx.dao.UserDaoInterface;
import com.cyx.pojo.Student;
import com.cyx.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
//@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDaoInterface userDaoInterface;

    @RequestMapping("/login")
    public String login(HttpServletRequest request,int userId, String password, String role) {
        System.out.println(role);
        if (role.equals("1")) {//学生登录
            Student student = userDaoInterface.findStudentByStudentId(userId);
            if (student == null || !(student.getPassword().equals(password))) {
                return "loginError";
            }
            System.out.println("-------------------");
            request.getSession().setAttribute("student",student);
            return "studentIndex";

        }
        if(role.equals("2")) {//老师登录
            Teacher teacher=userDaoInterface.findTeacherById(userId);
            if (teacher == null || !(teacher.getPassword().equals(password))) {
                return "loginError";
            }
            request.getSession().setAttribute("teacher",teacher);
            return "teacherIndex";
        }
        return "login";
    }

    @RequestMapping(value="/register",method=RequestMethod.POST)
    public String register(Student student) {// 可以直接通过javaBean对象传递
        System.out.println(1);
        userDaoInterface.addStudent(student);
        return "login";
    }
    @RequestMapping("/indexlogin")
    public String indexlogin(){// 可以直接通过javaBean对象传递
            return "login";
    }

}
